package com.lt.flashcard.view;

import java.util.Map;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.metainfo.ComponentInfo;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zul.Window;

import com.lt.flashcard.Utility;
import com.lt.flashcard.model.entity.User;
import com.lt.flashcard.model.service.LearningService;
import com.lt.flashcard.model.service.Repository;

public class DlgCompare extends SelectorComposer<Window> {

	private static final long serialVersionUID = 1L;
    private final int height = 195; // the height of column
	private int y_learned;
    private int y_point;
	private int y_item;
	private int y_medal;
	private int y_level;
	private int f_learned;
    private int f_point;
	private int f_item;
	private int f_medal;
	private int f_level;
	
	private String f_name = "";

	@Override
	public ComponentInfo doBeforeCompose(Page page, Component parent, ComponentInfo compInfo) {
		Map<String, User> arg = (Map<String, User>) Executions.getCurrent().getArg();
        User you = arg.get("you");
        User friend = arg.get("friend");
		
		LearningService lsrv = Repository.getInstance().getLearningService();
		
		y_learned = you.getKnowCards();
        y_point = you.getBlueCoin();
		y_item = lsrv.countJourneyItemsOfUser(you.getId());
		y_medal = lsrv.countMedalsOfUser(you.getId());
		y_level = Utility.getRank(you);
		
		f_learned = friend.getKnowCards();
        f_point = friend.getBlueCoin();
		f_item = lsrv.countJourneyItemsOfUser(friend.getId());
		f_medal = lsrv.countMedalsOfUser(friend.getId());
		f_level = Utility.getRank(friend);
		
		f_name = friend.getUsername();
		
		return super.doBeforeCompose(page, parent, compInfo);
	}
	
	public int calculate1(int x, int y) {
		if(x == 0 && y == 0)
			return 15;
		else if(x == 0)
			return 15;
		else if(y == 0)
			return height;
		else if(x > y)
			return height;
        else {
            int r = x * height / y;
            return r < 15 ? 15 : r;
        }
	}
	
	public int calculate2(int x, int y) {
		if(x == 0 && y == 0)
			return 15;
		else if(x == 0)
			return height;
		else if(y == 0)
			return 15;
        else if (x > y) {
            int r = y * height / x;
            return r < 15 ? 15 : r;
        } else
			return height;
	}

	public int getY_learned() {
		return y_learned;
	}

	public void setY_learned(int y_learned) {
		this.y_learned = y_learned;
	}

	public int getY_item() {
		return y_item;
	}

	public void setY_item(int y_item) {
		this.y_item = y_item;
	}

	public int getY_medal() {
		return y_medal;
	}

	public void setY_medal(int y_medal) {
		this.y_medal = y_medal;
	}

	public int getY_level() {
		return y_level;
	}

	public void setY_level(int y_level) {
		this.y_level = y_level;
	}

	public int getF_learned() {
		return f_learned;
	}

	public void setF_learned(int f_learned) {
		this.f_learned = f_learned;
	}

	public int getF_item() {
		return f_item;
	}

	public void setF_item(int f_item) {
		this.f_item = f_item;
	}

	public int getF_medal() {
		return f_medal;
	}

	public void setF_medal(int f_medal) {
		this.f_medal = f_medal;
	}

	public int getF_level() {
		return f_level;
	}

	public void setF_level(int f_level) {
		this.f_level = f_level;
	}

	/**
	 * @return the f_name
	 */
	public String getF_name() {
		return f_name;
	}

	/**
	 * @param f_name the f_name to set
	 */
	public void setF_name(String f_name) {
		this.f_name = f_name;
	}

    /**
     * @return the y_point
     */
    public int getY_point() {
        return y_point;
    }

    /**
     * @param y_point
     *            the y_point to set
     */
    public void setY_point(int y_point) {
        this.y_point = y_point;
    }

    /**
     * @return the f_point
     */
    public int getF_point() {
        return f_point;
    }

    /**
     * @param f_point
     *            the f_point to set
     */
    public void setF_point(int f_point) {
        this.f_point = f_point;
    }
}
